package top.healthylife.gateway.core.config;

import io.swagger.annotations.ApiModel;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import top.heathylife.gateway.common.po.GatewayLogPo;

import java.util.HashMap;
import java.util.Map;

/**
 * @author : GaoZeXi
 * @date 2021/10/14 14:38
 */
@Data
@ApiModel("数据网关常量")
@Slf4j
public class ApiGatewayConstant {
    public static final Map<String, Long> DEFAULT_PERIOD_VALIDITY = new HashMap<>();

    static {
        DEFAULT_PERIOD_VALIDITY.put("tyc", 3 * 24 * 60 * 60L);//T+3
        DEFAULT_PERIOD_VALIDITY.put("br", 24 * 60 * 60L);//T+1
        DEFAULT_PERIOD_VALIDITY.put("axsign", 24 * 60 * 60L);//T+1
        DEFAULT_PERIOD_VALIDITY.put("bank", -1L);
        DEFAULT_PERIOD_VALIDITY.put("bj", 24 * 60 * 60L);//T+1
        DEFAULT_PERIOD_VALIDITY.put("bhzx", 24 * 60 * 60L);//T+1  T+3
        DEFAULT_PERIOD_VALIDITY.put("chain", 24 * 60 * 60L);
        //DEFAULT_PERIOD_VALIDITY.put("fpd", -1L);
        DEFAULT_PERIOD_VALIDITY.put("geo", 24 * 60 * 60L);//T+1
        DEFAULT_PERIOD_VALIDITY.put("lexin", 24 * 60 * 60L);//T+1
        DEFAULT_PERIOD_VALIDITY.put("inner_black", -1L);
        DEFAULT_PERIOD_VALIDITY.put("wind", -1L);//T+0
        DEFAULT_PERIOD_VALIDITY.put("td", 24 * 60 * 60L);//T+1
        DEFAULT_PERIOD_VALIDITY.put("tc", 24 * 60 * 60L);//T+1
        DEFAULT_PERIOD_VALIDITY.put("baofu", 24 * 60 * 60L);//T+1
        DEFAULT_PERIOD_VALIDITY.put("kjt", 24 * 60 * 60L);//T+1
        DEFAULT_PERIOD_VALIDITY.put("jg", 3 * 24 * 60 * 60L);//T+3
        DEFAULT_PERIOD_VALIDITY.put("youshu", 24 * 60 * 60L);// T+1  T+3
        DEFAULT_PERIOD_VALIDITY.put("zzc", 24 * 60 * 60L);//T+1
    }

    /**
     * 获取各个数据源的默认缓存时间
     *
     * @param shortName 数据源简称
     * @return 默认缓存时间
     */
    public static Long getDefaultPeriodValidity(String shortName, GatewayLogPo gatewayLogPo) {
        Long aLong;
        if (!DEFAULT_PERIOD_VALIDITY.containsKey(shortName)) {
            //默认缓存时效性一小时
            log.info("当前请求数据源:{},尚未预置默认有效缓存时间,采取默认设置:{}秒", shortName, 3600L);
            aLong = 3600L;
            gatewayLogPo.setPeriodValidity(aLong+"C");
            return aLong;
        }
        aLong = DEFAULT_PERIOD_VALIDITY.get(shortName);
        gatewayLogPo.setPeriodValidity(aLong+"B");
        log.info("当前请求数据源:{},未传递默认有效缓存时间,已预置默认有效缓存时间:{}秒", shortName, aLong);
        return aLong;
    }
}
